#include<stdio.h>
#include<string.h>

void MakeTriangleType1(char triangle[], int lines)
{
    triangle[0]='\0';
    int i,j;
    for(i=0; i<lines; i++)
    {
        for(j=0; j<lines; j++)
        {
            if (j<=i)strcat(triangle,"*");
            else strcat(triangle," ");
        }
        strcat(triangle,"\n");
    }
}

void MakeTriangleType2(char triangle[], int lines)
{
    triangle[0]='\0';
    int i,j;
    for(i=0; i<lines; i++)
    {
        for(j=0; j<lines; j++)
        {
            if (j<lines-i-1)strcat(triangle," ");
            else strcat(triangle,"*");
        }
        strcat(triangle,"\n");
    }
}

void MakeTriangleType3(char triangle[], int lines)
{
    triangle[0]='\0';
    int i,j;
    for(i=0; i<lines; i++)
    {
        for(j=0; j<lines; j++)
        {
            if (j<lines-i)strcat(triangle,"*");
            else strcat(triangle," ");
        }
        strcat(triangle,"\n");
    }
}

void MakeTriangleType4(char triangle[], int lines)
{
    triangle[0]='\0';
    int i,j;
    for(i=0; i<lines; i++)
    {
        for(j=0; j<lines; j++)
        {
            if (j<i)strcat(triangle," ");
            else strcat(triangle,"*");
        }
        strcat(triangle,"\n");
    }
}


void ShowTriangleType(int lines, void (*PrintTriangle)(char triangle[], int lines))
{
    char triangle[256];
    PrintTriangle(triangle,lines);
    printf("%s",&triangle);
}

void ShowTriangle(triangleType,lines)
{
    switch( triangleType )
    {
    case 1:
        //ShowTriangleType1(lines);
        ShowTriangleType(lines, &MakeTriangleType1); //do like this
        break;

    case 2:
        ShowTriangleType(lines, &MakeTriangleType2);
        break;

    case 3:
        ShowTriangleType(lines, &MakeTriangleType3);
        break;

    case 4:
        ShowTriangleType(lines, &MakeTriangleType4);
        break;
    }
}

//We also can make function pointer for this
void WriteTriangleType(int lines,void (*MakeTriangleType) (char triangle[], int lines))
{
    FILE *fp;
    fp = fopen("c:\\test\\lab1.out","w");
    char triangle[256];
    MakeTriangleType(triangle,lines);
    fprintf(fp,"%s",triangle);
    fclose(fp);
}

void SaveTriangle(triangleType,lines)
{
    switch( triangleType )
    {
    case 1:
        WriteTriangleType(lines,&MakeTriangleType1);
        break;

    case 2:
        WriteTriangleType(lines,&MakeTriangleType2);
        break;

    case 3:
        WriteTriangleType(lines,&MakeTriangleType3);
        break;

    case 4:
        WriteTriangleType(lines,&MakeTriangleType4);
        break;
    }
    printf("File saved to c:\\test\\lab1.out successfully!\n");
}

void Display()
{
    char isAgain;
    do
    {
        int lines,triangleType;
        printf("WELCOME TO TRIANGLES HELL!!!\n- Select type of triangle you want to show (1-4): ");
        scanf("%d",&triangleType);

        while((triangleType<1)||(triangleType>4))
        {
            printf("Your input invalid. Re-select type of triangle you want to show (1-4): ");//Your input invalid. Re-select type of
            scanf("%d",&triangleType);
        }
        printf("- Enter a number of lines: ");
        scanf("%d",&lines);

        ShowTriangle(triangleType,lines);

        ;

        if (ConfirmText("Do you want to save to file (y/n)?","Your input invalid. Do you want to save to file (y/n)?")==1)
            SaveTriangle(triangleType,lines);
    }
    while (ConfirmText("Do you want to draw again (y/n)?","Your input invalid. Do you want to draw again (y/n)?")==1);
}

int ConfirmText(char firstText[], char secondText[])
{
    char confirm;
    printf("%s", firstText);
    fflush(stdin);
    scanf("%c", &confirm);
    while((confirm!='y')&&(confirm!='n')&&(confirm!='Y')&&(confirm!='N'))
    {
        printf("%s",secondText);
        fflush(stdin);
        scanf("%c", &confirm);
    }
    if ((confirm=='y')||(confirm=='Y'))
        return 1;
    else return 0;
}

void main()
{
    Display();
}
